package Frame.common;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * @author 陈思威 选项卡窗格
 *
 */
public class Tab extends JTabbedPane implements MouseListener, ChangeListener {
	private JPanel jp;
	private JLabel lab;
	private JPanel panel;
	private int index;

	/**
	 * 选项卡窗格 在controller类里实例化
	 */
	public Tab() {

	}

	public void addTTab(String label, JPanel jpanel) {
		JLabel lab = new JLabel(label + "  ");
		lab.setFont(new Font("华文楷体", Font.BOLD, 18));
		this.addTab(label, jpanel);
		this.setBounds(0, 86, 1920, 994);
		this.setOpaque(false);
		this.setVisible(true);
		jp = new JPanel();// 这个panel用来装两个label
		Icon icon = new ImageIcon(getClass().getResource("/logo/关闭.png"));
		JLabel labb = new JLabel();
		labb.addMouseListener(this);
		labb.setName("close"); 
		labb.setIcon(icon);
		lab.setIconTextGap(15);// 定义空格
		lab.setHorizontalTextPosition(SwingConstants.LEFT);
		jp.add(lab);// 文字的label
		jp.add(labb);// 图标的label
		this.setTabComponentAt(indexOfComponent(jpanel), jp); // 呈现标题和图标 ,indexOfComponent(jpanel)为返回指定组件的选项卡的索引。
		jp.setOpaque(false);
		this.setBackground(Color.white);//未被选中时的颜色
		this.setSelectedIndex(indexOfComponent(jpanel));//使新加的被选中
	}

	public int getIndex() {
		return index;
	}

	public JPanel getPanel() {
		return panel;
	}

	public void setPanel(JPanel panel) {
		this.panel = panel;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	@Override

	public void mouseClicked(MouseEvent e) {

		if ("close".equals(e.getComponent().getName())) {
			remove(indexOfTabComponent(e.getComponent().getParent()));
		}

	}

	@Override

	public void mouseEntered(MouseEvent e) {

		// TODO Auto-generated method stub

	}

	@Override

	public void mouseExited(MouseEvent arg0) {

		// TODO Auto-generated method stub

	}

	@Override

	public void mousePressed(MouseEvent arg0) {

		// TODO Auto-generated method stub

	}

	@Override

	public void mouseReleased(MouseEvent arg0) {

		// TODO Auto-generated method stub

	}

	@Override
	public void stateChanged(ChangeEvent arg0) {

	}

}
